package com.batchbrake.buttons;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.ImageIcon;

import com.batchbrake.main.BatchBrakeProc;

public class ProcessButton extends SkinnableButton implements Observer {
	private static ImageIcon[] disabled = new ImageIcon[] {
			new ImageIcon("images/encode/PanelButtonDisabledLeft.png"),
			new ImageIcon("images/encode/PanelButtonDisabledMiddle.png"),
			new ImageIcon("images/encode/PanelButtonDisabledRight.png")
	};
	private static ImageIcon[] normal = new ImageIcon[] {
			new ImageIcon("images/encode/PanelButtonNormalLeft.png"),
			new ImageIcon("images/encode/PanelButtonNormalMiddle.png"),
			new ImageIcon("images/encode/PanelButtonNormalRight.png")
	};
	private static ImageIcon[] over = new ImageIcon[] {
			new ImageIcon("images/encode/PanelButtonOverLeft.png"),
			new ImageIcon("images/encode/PanelButtonOverMiddle.png"),
			new ImageIcon("images/encode/PanelButtonOverRight.png")
	};
	private static ImageIcon[] pushed = new ImageIcon[] {
			new ImageIcon("images/encode/PanelButtonPushedLeft.png"),
			new ImageIcon("images/encode/PanelButtonPushedMiddle.png"),
			new ImageIcon("images/encode/PanelButtonPushedRight.png")
	};

	private ImageIcon[] redDisabled = new ImageIcon[] {
			new ImageIcon("images/encode/RedPanelButtonDisabledLeft.png"),
			new ImageIcon("images/encode/RedPanelButtonDisabledMiddle.png"),
			new ImageIcon("images/encode/RedPanelButtonDisabledRight.png")
	};
	private ImageIcon[] redPushed = new ImageIcon[] {
			new ImageIcon("images/encode/RedPanelButtonPushedLeft.png"),
			new ImageIcon("images/encode/RedPanelButtonPushedMiddle.png"),
			new ImageIcon("images/encode/RedPanelButtonPushedRight.png")
	};
	
	private boolean processLaunched = false;
	
	public ProcessButton(String title) {
		super(title, disabled, pushed, pushed, pushed);
	}

	public boolean isProcessLaunched() {
		return processLaunched;
	}

	public void setProcessLaunched(boolean processLaunched) {
		this.processLaunched = processLaunched;
		updateIcons();
	}
	
	public void updateIcons() {
		if( isProcessLaunched() ) {
			this.setIcons(redDisabled, redPushed, redPushed, redPushed);
		} else {
			this.setIcons(disabled, pushed, pushed, pushed);
		}
	}

	@Override
	public void update(Observable obs, Object obj) {
		if( obs instanceof BatchBrakeProc ) {
			setProcessLaunched(!((BatchBrakeProc) obs).isDone());
			if( isProcessLaunched() ) {
				setText("Stop encoding");
			} else {
				setText("Start encoding");
			}
		}
	}
}
